﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamBuilder.Common;
using ePlanet.SingleWell.Services;
using ePlanet.SingleWell.Models;
using ePlanet.SingleWell.DataAccess;
using Web.SystemConfig.WorkFlow;
using System.Data;
using Web.SystemConfig.DataAccess;

namespace ePlanet.SingleWell.Business
{
    public class BUSSingleWell : BusinessBase, ISingleWellService
    {
        private SingleWellDAl m_SingleWellDAL = null;

        public BUSSingleWell()
        {
            if (this.m_SingleWellDAL == null)
            {
                this.m_SingleWellDAL = new SingleWellDAl();
            }
        }

        #region  接口方法
        /// <summary>
        /// 新增或者修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string AddSingleWell(SingleWellModel model)
        {
            try
            {
                //this.TransSingleWellModel(model, false);
                if (this.m_SingleWellDAL.AddNewSingleWellModel(model))
                {
                    return model.SINGLEWELL[0].DATAID;
                }
                else
                {
                    throw new Exception("插入数据失败");
                }
            }
            catch
            {
                throw;
            }
        }

        public bool UpdateSingleWell(SingleWellModel model)
        {
            try
            {
                return true;
            }
            catch
            {
                throw;
            }
        }

        public bool DeleteSingleWell(SingleWellModel model)
        {
            try
            {
                return this.m_SingleWellDAL.DeleteNewSingleWellModel(model);
            }
            catch
            {
                throw;
            }
        }

        public bool DeleteSingleWell(string dataId)
        {
            try
            {
                return this.m_SingleWellDAL.DeleteNewSingleWellModel(dataId);
            }
            catch
            {
                throw;
            }
        }

        public SingleWellModel GetSingleWellModel(string wellDataID)
        {
            SingleWellModel rtnSingleWellModel = this.m_SingleWellDAL.GetSingleWellDataByWellDataID(wellDataID);
            if (rtnSingleWellModel != null && rtnSingleWellModel.SINGLEWELL.Rows.Count > 0)
            {
                //this.TransSingleWellModel(rtnSingleWellModel, true);
                return rtnSingleWellModel;
            }
            else
            {
                return rtnSingleWellModel;
            }
        }

        private void TransSingleWellModel(SingleWellModel singelModel, bool IsGetData)
        {
            if (singelModel != null && singelModel.SINGLEWELL.Rows.Count > 0)
            {
                DataTable wellTypeDt = this.GetSingleWellCodeTable(SingleWellCodeTableEnum.WellType);
                DataTable workModeDt = this.GetSingleWellCodeTable(SingleWellCodeTableEnum.WorkMode);
                if (!(wellTypeDt != null && wellTypeDt.Rows.Count > 0))
                {
                    throw new Exception("从码表获取井别失败");
                }
                if (!(workModeDt != null && workModeDt.Rows.Count > 0))
                {
                    throw new Exception("从码表获取施工措施失败");
                }

                foreach (SingleWellModel.SINGLEWELLRow row in singelModel.SINGLEWELL.Rows)
                {
                    if (IsGetData)
                    {
                        if (!row.IsWORKMODENull())
                        {
                            if (string.IsNullOrEmpty(this.GetSingleWellCodeTableValue(workModeDt, row.WORKMODE)))
                            {
                                row.WORKMODE = string.Empty;
                            }
                            else
                            {
                                row.WORKMODE = this.GetSingleWellCodeTableValue(workModeDt, row.WORKMODE);
                            }
                        }

                        if (string.IsNullOrEmpty(this.GetSingleWellCodeTableValue(wellTypeDt, row.WELLTYPE)))
                        {
                            row.WELLTYPE = string.Empty;
                        }
                        else
                        {
                            row.WELLTYPE = this.GetSingleWellCodeTableValue(wellTypeDt, row.WELLTYPE);
                        }
                    }
                    else
                    {
                        if (!row.IsWORKMODENull())
                        {
                            if (string.IsNullOrEmpty(this.GetSingleWellCodeTableElementID(workModeDt, row.WORKMODE)))
                            {
                                row.WORKMODE = string.Empty;
                            }
                            else
                            {
                                row.WORKMODE = this.GetSingleWellCodeTableElementID(workModeDt, row.WORKMODE);
                            }
                        }

                        if (!row.IsWELLTYPENull())
                        {
                            if (string.IsNullOrEmpty(this.GetSingleWellCodeTableElementID(wellTypeDt, row.WELLTYPE)))
                            {
                                row.WELLTYPE = string.Empty;
                            }
                            else
                            {
                                row.WELLTYPE = this.GetSingleWellCodeTableElementID(wellTypeDt, row.WELLTYPE);
                            }
                        }
                    }
                }
            }
        }

        public System.Data.DataTable GetAllSingleWell()
        {
            WorkFlow oWorkFlow = new Web.SystemConfig.WorkFlow.WorkFlow();
            return this.m_SingleWellDAL.GetAllSingleWellData();
        }

        public System.Data.DataTable ExecSql(string sql)
        {
            return this.m_SingleWellDAL.ExecSql(sql);
        }

        /// <summary>
        /// 数据需要从入库数据中取得。
        /// </summary>
        /// <param name="wellType"></param>
        /// <param name="beginNum"></param>
        /// <param name="count"></param>
        /// <param name="sum"></param>
        /// <returns></returns>
        public System.Data.DataTable GetSingleWellDataByWellType(string wellType, int beginNum, int count, out int sum, string userID)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            DataTable rtnDataTable = null;
            string filter = "wellcatalogid='" + wellType + "'";
            rtnDataTable = oWorkFlow.GetTotal(ArchiveTableEnum.SingleWell, beginNum, count, out sum, filter, userID);
            return rtnDataTable;
        }

        public DataTable GetSingleWellCodeTable(SingleWellCodeTableEnum codeTableEnum)
        {
            DALCodeTable oDALCodeTable = new DALCodeTable();
            DataSet oDataSet = oDALCodeTable.GetCodeTableDataByType(codeTableEnum.ToString());
            if (oDataSet != null && oDataSet.Tables.Count > 0)
            {
                return oDataSet.Tables[0];
            }
            else
            {
                return null;
            }
        }

        public SingleWellModel GetAgent(string userID, int beginNum, int count, ref int sum)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            SingleWellModel rtnModel = new SingleWellModel();
            DataTable oDataTable = oWorkFlow.GetAgent(Web.SystemConfig.WorkFlow.ArchiveTableEnum.SingleWell, userID, beginNum, count, string.Empty, ref sum);
            rtnModel.Merge(oDataTable);
            return new SingleWellModel();
        }

        public SingleWellModel GetDone(string userID, int beginNum, int count, ref int sum)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            SingleWellModel rtnModel = new SingleWellModel();
            DataTable oDataTable = oWorkFlow.GetDone(Web.SystemConfig.WorkFlow.ArchiveTableEnum.SingleWell, userID, beginNum, count, string.Empty, ref sum);
            rtnModel.Merge(oDataTable);
            return rtnModel;
        }

        public SingleWellModel GetTotal(int beginNum, int count, out int sum, string filter, string userID)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            SingleWellModel rtnModel = new SingleWellModel();
            DataTable oDataTable = oWorkFlow.GetTotal(Web.SystemConfig.WorkFlow.ArchiveTableEnum.SingleWell, beginNum, count, out  sum, filter, userID);
            rtnModel.SINGLEWELL.Merge(oDataTable);
            return rtnModel;
        }
        #endregion

        #region 私有方法

        private string GetSingleWellCodeTableValue(DataTable codeTable, string elementID)
        {
            if (codeTable != null && codeTable.Rows.Count > 0)
            {
                DataRow[] rows = codeTable.Select("CODETABLEID='" + elementID + "'");
                if (rows != null && rows.Length > 0)
                {
                    return rows[0]["CODE_TYPE"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

        private string GetSingleWellCodeTableElementID(DataTable codeTable, string elementName)
        {
            if (codeTable != null && codeTable.Rows.Count > 0)
            {
                DataRow[] rows = codeTable.Select("CODE_DESC='" + elementName + "'");
                if (rows != null && rows.Length > 0)
                {
                    return rows[0]["CODETABLEID"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

        #endregion

        #region ISingleWellService 成员


        public DataTable GetPageSingleWellByCatalogId(PageInfo pageinfo, string wellCatalogId, string orderBy)
        {
            return this.m_SingleWellDAL.GetPageSingleWellByCatalogId(pageinfo, wellCatalogId, orderBy);
        }

        #endregion


        public string GetAttachIDByDetailID(string detaileID, string DBHelperString)
        {
            return this.m_SingleWellDAL.GetAttachIDByDetailID(detaileID, DBHelperString);
        }


        public void DeleteDetail(string detailID)
        {
            this.m_SingleWellDAL.DeleteDetail(detailID);
        }

        #region ISingleWellService 成员

        /// <summary>
        /// 资源申请
        /// added by jiejiep 2013年4月9日 23:30:45
        /// </summary>
        /// <param name="filter">查询过滤条件</param>
        /// <param name="beginNum"></param>
        /// <param name="count"></param>
        /// <param name="sum"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable QuerySingleWell(string filter, int beginNum, int count, out int sum, string userId)
        {
            WorkFlow oWorkFlow = new WorkFlow();
            return oWorkFlow.GetTotal(ArchiveTableEnum.SingleWell, beginNum, count, out sum, filter, userId);
        }

        public DataTable StatisticsSinglewellWorkMode(int pageSize, int pageIndex, out int recordCount)
        {
            return this.m_SingleWellDAL.StatisticsSinglewellWorkmode(pageSize, pageIndex, out recordCount);
        }

        public DataTable StatisticsSinglewellWorkMode()
        {
            return this.m_SingleWellDAL.StatisticsSinglewellWorkmode();
        }

        public DataTable StatisticsSinglewell(string startDate, string endDate)
        {
            return this.m_SingleWellDAL.StatisticsSinglewell(startDate, endDate);
        }

        #endregion        
    }
}